import entity.News;
import model.Predict;
import model.Train;
import org.apache.log4j.Logger;
import utils.ClfyObj;
import utils.IOUtils;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.List;
import java.util.Properties;

/**
 * Created by 梅晨 on 2017/7/19.
 */
public class Main {
    private static Logger logger = Logger.getLogger(Main.class);
    private static String emoitonTrainDataPath;
    private static String emotionTestDataPath;
    private static String emotionModelPath;
    private static String emotionDicPath;
    private static String emotionTrainLibSVMPath;
    private static String emotionTestLibSVMPath;
    private static String emotionAccuracyFilePath;
    private static String emotionmapPath;
    private static String categoryTrainDataPath;
    private static String categoryTestDataPath;
    private static String categoryModelPath;
    private static String categoryDicPath;
    private static String categoryTrainLibSVMPath;
    private static String categoryTestLibSVMPath;
    private static String categoryAccuracyFilePath;
    private static String categorymapPath;
    static {
        Properties properties = new Properties();
        try{
            logger.info("load configuration from properties");
            String today = new java.sql.Date(new java.util.Date().getTime()).toString();
            properties.load(new InputStreamReader(new FileInputStream("src\\main\\resources\\PathConfig.properties"),"UTF-8"));
            emoitonTrainDataPath = properties.getProperty("emotionTrainDataPath");
            emotionTestDataPath = properties.getProperty("emorionTestDataPath") + today;
            emotionModelPath = properties.getProperty("emorionModelPath");
            emotionDicPath = properties.getProperty("emorionDicPath");
            emotionTrainLibSVMPath = properties.getProperty("emorionTrainLibSVMPath");
            emotionTestLibSVMPath = properties.getProperty("emorionTestLibSVMPath");
            emotionAccuracyFilePath = properties.getProperty("emorionAccuracyFilePath");
            emotionmapPath = properties.getProperty("emotionMapPath");
            categoryTrainDataPath = properties.getProperty("categoryTrainDataPath");
            categoryTestDataPath = properties.getProperty("categoryTestDataPath") + today;
            categoryModelPath = properties.getProperty("categoryModelPath");
            categoryDicPath = properties.getProperty("categoryDicPath");
            categoryTrainLibSVMPath = properties.getProperty("categoryTrainLibSVMPath");
            categoryTestLibSVMPath = properties.getProperty("categoryTestLibSVMPath");
            categoryAccuracyFilePath = properties.getProperty("categoryAccuracyFilePath");
            categorymapPath = properties.getProperty("categorymapPath");

            logger.info("load configuration finished");
        }catch (IOException e){
            logger.trace(e);
        }
    }
    public static void main(String[] args)throws IOException{
        String k = "p";
        if(k == "t"){
            //训练正负向情绪数据模型
            logger.info("loading emotion traindata");
            HashMap<String,Integer> emotionTrainData = IOUtils.getFilePathClassMap(emoitonTrainDataPath,emotionmapPath);
            logger.info("start train emotion");
            Train emotiontrain = new Train(emotionTrainData,emotionModelPath,emotionDicPath,emotionTrainLibSVMPath);
            emotiontrain.run();
            logger.info("emotion train job finished");

//            //训练不同行业类目数据模型
//            logger.info("loading category traindata");
//            HashMap<String,Integer> categoryTrainData = IOUtils.getFilePathClassMap(categoryTrainDataPath,categorymapPath);
//            logger.info("start train category");
//            Train categorytrain = new Train(categoryTrainData,categoryModelPath,categoryDicPath,categoryTrainLibSVMPath);
//            categorytrain.run();
//            logger.info("category train job finished");

        } else if (k == "p") {
            logger.info("loading emotion testdata");
            HashMap<String,Integer> emotionTestData = IOUtils.getFilePathClassMap(emotionTestDataPath,null);
            logger.info("start predict emotion");
            Predict emotionpredict = new Predict(emotionTestData,emotionModelPath,emotionDicPath,emotionTestLibSVMPath,emotionTrainLibSVMPath,emotionAccuracyFilePath);
            emotionpredict.run();
            logger.info("emotion predict job finished");

            logger.info("loading category traindata");
            HashMap<String,Integer> categoryTestData = IOUtils.getFilePathClassMap(categoryTestDataPath,null);
            logger.info("start pridict category");
            Predict categorypredict = new Predict(categoryTestData,categoryModelPath,categoryDicPath,categoryTestLibSVMPath,categoryTrainLibSVMPath,categoryAccuracyFilePath);
            categorypredict.run();
            logger.info("category predict job finished");
            ClfyObj clfyObj = new ClfyObj(categoryTestData);
            clfyObj.newObj();
        }


    }
}
